package ouhenry.stage.gateway.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
@EnableAutoConfiguration
public class SwaggerConfig {
    private static final Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);
//    @Value("${swagger.enable}")
    private boolean enable=true;

    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi() {
        logger.info("swagger创建API开启结果{}", enable);
        return new Docket(DocumentationType.OAS_30)
                // 详细定制
                .apiInfo(apiInfo("1.0.0"))
                // 是否启用swagger文档
                .enable(enable).select()
                // 指定当前包路径
                .apis(RequestHandlerSelectors.basePackage("ouhenry.stage"))
                // 扫描所有
                .paths(PathSelectors.any()).build();
        // 记一笔：swagger在初始化显示时，如果response body中有时间，那么它显示的会是无时区的时间，但是查询结果不会受到影响
        // .directModelSubstitute(LocalDateTime.class, String.class);
    }

    /**
     * 添加摘要信息
     * 
     * @param version
     * @return
     */
    private ApiInfo apiInfo(String version) {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder().title("脚手架接口文档").version(version).build();
    }
}
